package by.airlanterns.web;

import by.airlanterns.dto.User;
import by.airlanterns.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Created by IntelliJ IDEA.
 * User: Sergey
 * Date: 29.01.13
 * Time: 4:34
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "auth", method = RequestMethod.GET)
    private ModelAndView showAuthorize(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("auth");
    }

    @RequestMapping(value = "registration", method = RequestMethod.GET)
    private ModelAndView showRegistration(User user, HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("registration");
    }

    @RequestMapping(value = "register_user", method = RequestMethod.POST)
    private ModelAndView registerUser(@Valid User user, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
        if (result.hasErrors()) {
            return new ModelAndView("registration");
        } else {
            String pass = user.getPassword();
            PasswordEncoder encoder = new Md5PasswordEncoder();
            String hashedPass = encoder.encodePassword(user.getPassword(), null);
            user.setPassword(hashedPass);
            user.setLevel(ROLE_USER);
            user.setToken("");
            int status = userService.saveUser(user);
            switch (status) {

                case UserService.STATUS_EMAIL:
                    return new ModelAndView("redirect:/user/registration.html?error=email");

                case UserService.STATUS_LOGIN:
                    return new ModelAndView("redirect:/user/registration.html?error=login");

                case UserService.STATUS_OK:
                    Authentication authentication = new UsernamePasswordAuthenticationToken(user.getLogin(), pass);
                    SecurityContextHolder.getContext().setAuthentication(authentication);
                    return new ModelAndView("redirect:/index.html");

            }
            return new ModelAndView("redirect:/index.html");
        }
    }


}
